package com.ltmonitor.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
/**
 * 根据实体类自动生成Dao类和Service类及他们的接口
 * @author admin
 *
 */
public class CodeGenerate {

	public static BufferedReader bufread;
	// 指定文件路径和名称
	private static String path = "E:/template/";	
	public static void replace(String entityName, String lowerEntityName)
			throws IOException {
		String templName = "ITemplateDao.java";
		String strContent = readTxtFile(templName);
		
		String basePath = "E:/gps-src-mysql/gps-domain/src/main/java/com/ltmonitor/";

		String newStr = strContent.replaceAll("EntityName", entityName);
		newStr = newStr.replaceAll("entityName", lowerEntityName);
		String newFileName = "I" + entityName + "Dao.java";
		writeTxtFile(basePath + "dao/", newStr, newFileName);
		

		templName = "TemplateDao.java";
		strContent = readTxtFile(templName);
		newStr = strContent.replaceAll("EntityName", entityName);
		newStr =newStr.replaceAll("entityName", lowerEntityName);
		newFileName = entityName + "Dao.java";
		writeTxtFile(basePath + "dao/impl/",newStr, newFileName);
		

		templName = "ITemplateService.java";
		strContent = readTxtFile(templName);
		newStr = strContent.replaceAll("EntityName", entityName);
		newStr =newStr.replaceAll("entityName", lowerEntityName);
		newFileName =  "I" + entityName + "Service.java";
		writeTxtFile(basePath + "service/",newStr, newFileName);
		


		templName = "TemplateService.java";
		strContent = readTxtFile(templName);
		newStr = strContent.replaceAll("EntityName", entityName);
		newStr =newStr.replaceAll("entityName", lowerEntityName);
		newFileName =   entityName + "Service.java";
		writeTxtFile(basePath + "service/impl/",newStr, newFileName);
		
		
	}
	
	


	public static void main(String[] args) throws IOException {
		replace("PlatformInfo", "platformInfo");
	}
	

	public static String readTxtFile(String templateName) {
		File filename = new File(path + templateName);
		String read;
		FileReader fileread;
		String readStr = "";
		try {
			fileread = new FileReader(filename);
			bufread = new BufferedReader(fileread);
			try {
				while ((read = bufread.readLine()) != null) {
					readStr = readStr + read + "\r\n";
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return readStr;
	}

	public static void writeTxtFile(String filePath, String newStr, String codeFileName)
			throws IOException {

		System.out.println("文件内容是:" + "\r\n" + newStr);
		File filename = new File(filePath + codeFileName);
		// 先读取原有文件内容，然后进行写入操作
		String filein = newStr ;
		RandomAccessFile mm = null;
		try {
			mm = new RandomAccessFile(filename, "rw");
			mm.writeBytes(filein);
		} catch (IOException e1) {
			// TODO 自动生成 catch 块
			e1.printStackTrace();
		} finally {
			if (mm != null) {
				try {
					mm.close();
				} catch (IOException e2) {
					// TODO 自动生成 catch 块
					e2.printStackTrace();
				}
			}
		}
	}

	public static void replaceTxtByStr(String oldStr, String replaceStr) {
		String temp = "";
		try {
			File file = new File(path);
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			StringBuffer buf = new StringBuffer();

			// 保存该行前面的内容
			for (int j = 1; (temp = br.readLine()) != null
					&& !temp.equals(oldStr); j++) {
				buf = buf.append(temp);
				buf = buf.append(System.getProperty("line.separator"));
			}

			// 将内容插入
			buf = buf.append(replaceStr);

			// 保存该行后面的内容
			while ((temp = br.readLine()) != null) {
				buf = buf.append(System.getProperty("line.separator"));
				buf = buf.append(temp);
			}

			br.close();
			FileOutputStream fos = new FileOutputStream(file);
			PrintWriter pw = new PrintWriter(fos);
			pw.write(buf.toString().toCharArray());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
